/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * <p>Project: javase - Img4
 * <p>Powered by webrx On 2021-12-17 14:27:45
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Img4 {
    /**
     * 功能：根据文件TTF文件，建立Font对象
     * url = fonts/xx.ttf
     * url = c:/yy.ttf
     *
     * @param url
     * @param style
     * @param size
     * @return
     */
    public static Font getFont(String url, int style, int size) {
        var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(url);
        Font font = new Font("", style, size);
        try {
            font = Font.createFont(0, is).deriveFont(Font.BOLD, (float) size);
        } catch (Exception e) {
        }
        return font;
    }

    public static void main(String[] args) throws IOException, FontFormatException {
        int w = 500;
        int h = 480;
        var img = new BufferedImage(w, h, 2);
        var g = img.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        g.rotate(.01);
        g.setFont(getFont("fonts/et1.ttf", Font.BOLD, 30));
        g.setColor(Color.WHITE);
        g.drawString("Hello,李四，河南省郑州市科学大道。", 28, 48);

        g.setColor(Color.BLACK);
        g.drawString("Hello,李四，河南省郑州市科学大道。", 32, 52);

        g.setColor(Color.RED);
        g.drawString("Hello,李四，河南省郑州市科学大道。", 30, 50);
        g.dispose();

        ImageIO.write(img, "png", new File("c:/t.png"));
        Runtime.getRuntime().exec("cmd /k start c:/t.png");
    }
}
